﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AltimilliaLibrary
{
  public class AnimationData
  {
    public Rectangle SpriteSheetDim { get; set; }
    public int FrameWidth { get; set; }
    public int FrameHeight { get; set; }
    public Texture2D Texture { get; set; }
    public List<Rectangle> Rectangles { get; set; }
    private int rows;
    private int columns;
    public Dictionary<String, List<Frame>> Behavior {get;set;}

    public AnimationData(Rectangle ssheetDimension, int FrameWidth, int FrameHeight,int columns,int rows, Texture2D Texture,Dictionary<string,List<Frame>> Behaviours)
    {
      this.SpriteSheetDim = ssheetDimension;
      this.FrameHeight = FrameHeight;
      this.FrameWidth = FrameWidth;
      this.Texture = Texture;
      this.rows = rows;
      this.columns = columns;
      Rectangles = new List<Rectangle>();
      Behavior = Behaviours;
      ProcessSpriteSheet();
      ProcessBehaviours();
    }

    private void ProcessSpriteSheet()
    {
      Point Origin = new Point(SpriteSheetDim.X,SpriteSheetDim.Y);

      for (int i = 0; i < SpriteSheetDim.Height; i += FrameHeight)
      {
        for (int j = 0; j < SpriteSheetDim.Width; j += FrameWidth)
        {
          Rectangles.Add(new Rectangle(SpriteSheetDim.X + j * FrameWidth, SpriteSheetDim.Y + i * FrameHeight, FrameWidth, FrameHeight));
        }
      }
    }

    private Rectangle Frame(int Position)
    {
      return new Rectangle(
        ((Position - 1) % columns) * FrameWidth,
        ((Position - 1) / columns) * FrameHeight,
        FrameWidth,
        FrameHeight);
    }

    private void ProcessBehaviours()
    {
      foreach (KeyValuePair<String, List<Frame>> val in Behavior)
      {
        foreach (Frame f in val.Value)
        {
          f.Rectangle = Frame(f.index);
        }
      }
    }
  }
}
